package com.ziyun.pop.rest.module.goods;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.utility.JsonUtils;
import com.utility.New;
import com.utility.http.HttpUtils;
import com.ziyun.pop.common.utils.UserUtils;
import com.ziyun.pop.modules.goods.TwbVo.TwbGoodsListVo;
import com.ziyun.pop.modules.goods.TwbVo.TwbStateCollectVo;
import com.ziyun.pop.modules.goods.entity.GoodsEntity;
import com.ziyun.pop.modules.goods.service.GoodsService;
import com.ziyun.pop.modules.mall.service.PurchaseGoodsService;
import com.ziyun.pop.modules.mall.vo.PurchaseGoodsVo;
import io.renren.common.utils.R;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(value = "GoodsRestApi", tags = "商品服务")
@RestController
@RequestMapping("popApi/goods/goods/")
public class GoodsRestApi {

    @Value("${erpApi}")
    private String erpApi ;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private PurchaseGoodsService purchaseGoodsService;

//    @ApiOperation(value = "图文宝小程序商品列表")
//    @ApiResponses(value = @ApiResponse(code = 200, message = "操作成功"))
//    @PostMapping("/getTwbGoodsList")
//    public R getTwbGoodsList(@RequestBody Map params){
//        Map<String, Object> resultMap = getTwbGoodsListHandler(params);
//        return R.apiOk(JsonUtils.object2String(resultMap.get("goodsEntityPage")));
//    }

    private Page<TwbGoodsListVo> getTwbGoodsListHandler(String marketable,Integer current,Integer size) {
        String path ="/goods/goods/getTwbGoodsList";
        Map<String, Object> map = New.hashMap();
        map.put("sellerId",UserUtils.getCurentUser().getSellerId());
        map.put("marketable",marketable);
        map.put("current",current);
        map.put("size",size);
        Page<TwbGoodsListVo> twbGoodsList = goodsService.getTwbGoodsList(map);
        return twbGoodsList;
    }

    @ApiOperation(value = "图文宝小程序商品列表批量上下架操作")
    @ApiResponses(value = @ApiResponse(code = 200, message = "操作成功"))
    @PostMapping("/twbListOperation")
    public R twbListOperation(@RequestBody Map params){
        twbListOperationHandler(params);
        return R.ok();
    }

    private void twbListOperationHandler(Map params) {
        String path ="/goods/goods/twbListOperation";
        Map<String, Object> map = New.hashMap();
        map.put("goodIdList",params.get("goodIdList"));//List<Integer>
        map.put("marketable",params.get("marketable"));//String
        Map<String, Object> resultMap = remoteProcedureCall(map, path);
    }

//    @ApiOperation(value = "图文宝小程序商品状态统计")
//    @ApiResponses(value = @ApiResponse(code = 200, message = "操作成功"))
//    @PostMapping("/twbGoodsStateCount")
//    public R twbGoodsStateCount(@RequestBody Map params){
//        TwbStateCollectVo twbStateCollectVo = twbGoodsStateCountHandler(params);
//        return R.apiOk(JsonUtils.object2String(twbStateCollectVo));
//    }

    private TwbStateCollectVo twbGoodsStateCountHandler() {
        String path ="/goods/goods/twbGoodsStateCount";
        Map<String, Object> map = New.hashMap();
        map.put("sellerId",UserUtils.getCurentUser().getSellerId());//List<Integer>
        Map<String, Object> resultMap = remoteProcedureCall(map, path);
        String twbStateCollectVoStr = JsonUtils.object2String(resultMap.get("twbStateCollectVo"));
        return JsonUtils.string2Object(twbStateCollectVoStr, TwbStateCollectVo.class);
    }

//    @ApiOperation(value = "图文宝小程序热销商品")
//    @ApiResponses(value = @ApiResponse(code = 200, message = "操作成功"))
//    @PostMapping("/twbHostGoods")
//    public R twbHostGoods(@RequestBody Map params){
//        List<GoodsEntity> hostGoodsList = twbHostGoodsHandler(params);
//        return R.apiOk(JsonUtils.object2String(hostGoodsList));
//    }

    private List<GoodsEntity> twbHostGoodsHandler() {
        Long sellerId = UserUtils.getCurentUser().getSellerId();
        return goodsService.twbHostGoods(sellerId);
    }

    @ApiOperation(value = "图文宝小程序商品页面全部数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "marketable", value = "上下架状态", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "current", value = "当前页", required = true, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "size", value = "每页数", required = true, paramType = "query", dataType = "Integer")
    })
    @ApiResponses(value = @ApiResponse(code = 200, message = "操作成功"))
    @GetMapping("/twbAppletAllGoodsData")
//    public R twbAppletAllGoodsData(@RequestBody Map params){
    public R getTwbAppletAllGoodsData(String marketable,Integer current,Integer size) throws Exception{
        //热销商品
        List<GoodsEntity> hostGoodsList = twbHostGoodsHandler();
        //图文宝商品分页结果
        Page<TwbGoodsListVo> twbGoodsListHandler = getTwbGoodsListHandler(marketable, current, size);
//        String goodsEntityPage = JsonUtils.object2String(twbGoodsPageList.get("goodsEntityPage"));
//        Map<String, Object> map = JsonUtils.string2Map(goodsEntityPage);
//        int total = (int) map.get("total");
//        int current1 = (int) map.get("current");
//        int size1 = (int) map.get("size");
//        int pages1 = (int) map.get("pages");
//        String records1 = JsonUtils.object2String(map.get("records"));
//        List records = JsonUtils.string2Collection(JsonUtils.object2String(map.get("records")), List.class, GoodsEntity.class);
//        PageUtils pageUtils = new PageUtils(records,total,size1,current1);
        //图文宝商品上下架状态数量统计
        TwbStateCollectVo twbStateCollectVo = twbGoodsStateCountHandler();
        Map<String, Object> resultMap = New.hashMap();
        resultMap.put("hostGoodsList",hostGoodsList);
        resultMap.put("twbGoodsPageList",twbGoodsListHandler);
        resultMap.put("twbStateCollectVo",twbStateCollectVo);
        return R.apiOk(resultMap);
    }

    /**
     * 远程调用方法封装
     * @param params
     * @return
     */
    private Map<String, Object> remoteProcedureCall(@RequestParam Map<String, Object> params, String path) {
        String url = erpApi + path;
        Map<String, Object> paramHeaderMap = New.hashMap();
        Map<String, Object> paramBodyMap = New.hashMap();
        paramBodyMap.put("params",params);
        String response = HttpUtils.doPost(url, paramHeaderMap, JsonUtils.map2String(paramBodyMap));
        return com.ziyun.pop.common.utils.JsonUtils.jsonToMap(response);
    }

    @ApiOperation(value = "查询采购商品", notes = "")
    @ApiResponses(value = @ApiResponse(code = 200, message = "获取成功", response = PurchaseGoodsVo.class))
    @GetMapping("/getPurchaseGoods")
    public R getPurchaseGoods(){
        List<PurchaseGoodsVo> purchaseGoodsVos = purchaseGoodsService.queryGoodsList();
        return R.apiOk(purchaseGoodsVos);
    }

}
